

package com.mt.restaurant.controller.sale;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.restaurant.dto.sale.MenuSaleRecordEditDto;
import com.mt.restaurant.entity.sale.MenuSaleRecord;
import com.mt.restaurant.service.sale.MenuSaleRecordService;
import com.mt.restaurant.service.sale.VegetableMenuService;
import com.mt.restaurant.service.sale.SaleOrderService;


@Api(tags = "菜谱销售记录")
@RestController
@RequestMapping("/api/restaurant/sale/MenuSaleRecord")
@CrossOrigin(allowCredentials = "true")
public class MenuSaleRecordController {
    private static Logger logger = LoggerFactory.getLogger(MenuSaleRecordController.class);


    @Autowired
    private MenuSaleRecordService menuSaleRecordService;
    @Autowired
    private VegetableMenuService vegetableMenuService;
    @Autowired
    private SaleOrderService saleOrderService;

    /**
     * 根据分页参数查询菜谱销售记录集合
     *
     * @param pageDTO 分页条件
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("分页查询菜谱销售记录")
    @ApiPageParam
    @PostMapping("/findMenuSaleRecords")
    public PageResultDTO findMenuSaleRecords(@RequestBody PageDTO pageDTO) {
        return this.menuSaleRecordService.findMenuSaleRecords(pageDTO);
    }

    /**
     * 根据ID查询指定的菜谱销售记录
     *
     * @param menuSaleRecordId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("根据id查询菜谱销售记录")
    @ApiPageParam
    @PostMapping("/findMenuSaleRecord")
    public MenuSaleRecord findMenuSaleRecord(@RequestParam Long menuSaleRecordId) {
        return this.menuSaleRecordService.findMenuSaleRecord(menuSaleRecordId);
    }

    /**
     * 根据ID查询指定的菜谱销售记录(包含外键名称)
     *
     * @param menuSaleRecordId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("根据ID查询指定的菜谱销售记录(包含外键名称)")
    @PostMapping("/findMenuSaleRecordForView")
    public MenuSaleRecord findMenuSaleRecordForView(@RequestParam Long menuSaleRecordId) {
        return this.menuSaleRecordService.findMenuSaleRecordWithForeignName(menuSaleRecordId);
    }

    /**
     * 根据ID查询指定的菜谱销售记录(包含菜谱销售记录和外键名称)
     *
     * @param menuSaleRecordId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("根据ID查询指定的菜谱销售记录(包含菜谱销售记录和外键名称)")
    @PostMapping("/findMenuSaleRecordForEdit")
    public MenuSaleRecordEditDto findMenuSaleRecordForEdit(@RequestParam Long menuSaleRecordId) {
        MenuSaleRecordEditDto menuSaleRecordEditDto = new MenuSaleRecordEditDto();
        menuSaleRecordEditDto.setMenuSaleRecord(this.menuSaleRecordService.findMenuSaleRecordWithForeignName(menuSaleRecordId));

        this.prepareMenuSaleRecordEditDto(menuSaleRecordEditDto);

        return menuSaleRecordEditDto;
    }

    /**
     * 根据ID查询指定的菜谱销售记录(只提取ID 和 Name)
     *
     * @param menuSaleRecordId Id
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("根据ID查询指定的菜谱销售记录(只提取ID 和 Name)")
    @PostMapping("/findMenuSaleRecordsWithIdNameById")
    public MenuSaleRecord findMenuSaleRecordsWithIdNameById(@RequestParam Long menuSaleRecordId) {
        return this.menuSaleRecordService.findMenuSaleRecordsWithIdNameById(menuSaleRecordId);
    }

    /**
     * 根据名称查询菜谱销售记录集合(只提取ID 和 Name)
     *
     * @param menuSaleRecordName 名称
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:view')")
    @ApiOperation("根据名称查询菜谱销售记录集合(只提取ID 和 Name)")
    @PostMapping("/findMenuSaleRecordsWithIdNameByName")
    public List<MenuSaleRecord> findMenuSaleRecordsWithIdNameByName(@RequestParam String menuSaleRecordName) {
        return this.menuSaleRecordService.findMenuSaleRecordsWithIdNameByName(menuSaleRecordName);
    }


    /**
     * 创建新的菜谱销售记录
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:add')")
    @ApiOperation("创建新的菜谱销售记录")
    @PostMapping("/createMenuSaleRecord")
    public MenuSaleRecordEditDto createMenuSaleRecord() {
        MenuSaleRecordEditDto menuSaleRecordEditDto = new MenuSaleRecordEditDto();
        menuSaleRecordEditDto.setMenuSaleRecord(new MenuSaleRecord());

        this.prepareMenuSaleRecordEditDto(menuSaleRecordEditDto);
        return menuSaleRecordEditDto;
    }

    private void prepareMenuSaleRecordEditDto(MenuSaleRecordEditDto menuSaleRecordEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        menuSaleRecordEditDto.setMenuVegetableMenus(this.vegetableMenuService.findAllVegetableMenusWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        menuSaleRecordEditDto.setSaleOrderSaleOrders(this.saleOrderService.findAllSaleOrdersWithIdName());
    }

    /**
     * 新增保存菜谱销售记录
     *
     * @param menuSaleRecord 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:add')")
    @ApiOperation("新增保存菜谱销售记录")
    @PostMapping("/saveMenuSaleRecord")
    public MenuSaleRecord saveMenuSaleRecord(@RequestBody MenuSaleRecord menuSaleRecord) {
        return this.menuSaleRecordService.saveMenuSaleRecord(menuSaleRecord);
    }

    /**
     * 修改保存菜谱销售记录
     *
     * @param menuSaleRecord 实体对象
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:update')")
    @ApiOperation("修改保存菜谱销售记录")
    @PostMapping("/updateMenuSaleRecord")
    public MenuSaleRecord updateMenuSaleRecord(@RequestBody MenuSaleRecord menuSaleRecord) {
        return this.menuSaleRecordService.updateMenuSaleRecord(menuSaleRecord);
    }

    /**
     * 根据ID删除菜谱销售记录
     *
     * @param menuSaleRecordId ID
     */
    @PreAuthorize("hasAuthority('restaurant:sale:MenuSaleRecord:remove')")
    @ApiOperation("根据ID删除菜谱销售记录")
    @PostMapping("/deleteMenuSaleRecord")
    public void deleteMenuSaleRecord(@RequestParam Long menuSaleRecordId) {
        this.menuSaleRecordService.deleteMenuSaleRecord(menuSaleRecordId);
    }


}

